home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD-ROM Today - The Disc! 5
/
CD-ROM Today - The Disc (Issue 5)(November 1994).ISO
/
mac
/
Mac shareware
/
Education
/
RLaB
/
examples
/
collatz.r
< prev
next >
Wrap
Text File
|
1994-09-21
|
457b
|
35 lines
//
// The 3n + 1 problem
//
collatz = function(start)
{
local(c, n);
c = n = start;
while(n > 1)
{
if(mod(n,2) == 0) {
n = n/2;
else
n = 3*n + 1;
}
c = [c, n];
}
return c;
};
//
// Try it out
//
c = collatz(100);
plgrid ();
ptitle ( "RLaB Collatz Example" );
xlabel ( "Independent Variable" );
ylabel ( "Dependent Variable" );
plot( [1:c.nc; c]' );
pause ();
plgrid ( ,"bcgnstlv");
plot( [1:c.nc; c]' );